package com.ruoyi.activiti.controller;

import org.activiti.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.ruoyi.activiti.domain.dto.HistoryProcessInstanceDTO;
import com.ruoyi.activiti.service.IActTaskService;
import com.ruoyi.activiti.service.IActivitiHistoryService;
import com.ruoyi.activiti.service.IFormHistoryDataService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;

@RestController
@RequestMapping("/processInstance")
public class HistoryProcessInstanceCoroller extends BaseController {
    @Autowired
    private IFormHistoryDataService formHistoryDataService;
    @Autowired
    private IActivitiHistoryService activitiHistoryService;

    @Autowired
    private IActTaskService actTaskService;
    @Autowired
    private RuntimeService runtimeService;

    // 我的：已办
    @GetMapping("/list/{type}")
    public TableDataInfo getAll(@PathVariable("type") String type) {
        return this.getAllWithKey(type, null);
    }
    // 我的：已办：限制key
    @SuppressWarnings("resource")
	@GetMapping("/list/{type}/{processDefinitionKey}")
    public TableDataInfo getAllWithKey(@PathVariable("type") String type, @PathVariable("processDefinitionKey") String processDefinitionKey) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        if (processDefinitionKey==null || processDefinitionKey.isEmpty()) {
            processDefinitionKey = null;
        }
		Page<HistoryProcessInstanceDTO> hashMaps = new Page<HistoryProcessInstanceDTO>();
        switch (type) {
            case "all":
                hashMaps = activitiHistoryService.getProcessInstances(pageDomain, "all", processDefinitionKey);
                break;
            case "meCreated":
                hashMaps = activitiHistoryService.getProcessInstances(pageDomain, "meCreated", processDefinitionKey);
                break;
            case "startUserName":
                hashMaps = activitiHistoryService.getProcessInstances(pageDomain, "startUserName", processDefinitionKey);
                break;
            case "done":
                hashMaps = activitiHistoryService.getTasksDoneEx(pageDomain, processDefinitionKey);
                break;
            case "finished":
                hashMaps = activitiHistoryService.getProcessInstances(pageDomain, "done", processDefinitionKey);
                break;
            case "unfinished":
                hashMaps = actTaskService.selectProcessDefinitionList(pageDomain, processDefinitionKey);
                break;
            case "task":
                hashMaps = actTaskService.selectProcessDefinitionList(pageDomain, processDefinitionKey);
                break;
            default:
                hashMaps = activitiHistoryService.getProcessInstances(pageDomain, "all", processDefinitionKey);
                break;
        }
        return getDataTable(hashMaps);
    }
}
